import { ElMessage } from "element-plus";
import store from "../store";
import bus from "./bus";
import {
  SocketTypeEnum,
  findEnumNameByValue,
  ToWhomAll,
} from "./socketTypeEnum";

let websocket: WebSocket | null = null; // 用于存储实例化后websocket
let rec: any; // 断线重连后，延迟5秒重新创建WebSocket连接  rec用来存储延迟请求的代码
let heartBeatInterval: any | null = null;
const heartBeatMessage = "heartbeat";
const heartBeatIntervalTime = 60000; // 30 秒发送一次心跳
const reConnectIntervalTime = 5000; // 5 秒发送一次重新连接
let isConnected: boolean = false;
// 创建websocket
function creatWebSocket(wsUrl: string) {
  console.log("websocket==================");
  // 判断当前浏览器是否支持WebSocket
  if ("WebSocket" in window) {
    console.log("当前浏览器支持 WebSocket");
  } else if ("MozWebSocket" in window) {
    console.log("当前浏览器支持 MozWebSocket");
  } else {
    console.log("当前浏览器不支持 WebSocket");
  }
  try {
    initWebSocket(wsUrl); // 初始化websocket连接
  } catch (e) {
    console.log("尝试创建连接失败");
    reConnect(wsUrl); // 如果无法连接上 webSocket 那么重新连接！可能会因为服务器重新部署，或者短暂断网等导致无法创建连接
  }
}
let numHeartBeat: Number = 0;
// 初始化websocket
function initWebSocket(wsUrl: string) {
  websocket = new WebSocket(wsUrl);
  // console.log("websocket:", websocket);
  websocket.onopen = function () {
    websocketOpen();
  };
  //   // 接收
  websocket.onmessage = function (e: MessageEvent<any>) {
    websocketonmessage(e);
  };
  // 连接发生错误
  websocket.onerror = function () {
    console.log("WebSocket连接发生错误");
    //     isConnect = false; // 连接断开修改标识
    isConnected = false;
    reConnect(wsUrl); // 连接错误 需要重连
  };
  websocket.onclose = function (e) {
    console.log("WebSocket连接断开");
    isConnected = false;
    websocketclose(e);
    // 添加重新连接逻辑
    reConnect((websocket as any).url || "");
  };
}
// 定义重连函数
const reConnect = (wsUrl: string) => {
  console.log("尝试重新连接");
  // (store.state as any).websocket.
  if (isConnected) return; // 如果已经连上就不在重连了
  rec && clearTimeout(rec);
  rec = setTimeout(function () {
    clearHeartbeat();
    // 延迟5秒重连  避免过多次过频繁请求重连
    creatWebSocket(wsUrl);
  }, reConnectIntervalTime);
};

function sendHeartbeat() {
  if (websocket && isConnected) {
    numHeartBeat = 1;
    //  {
    //     Type: ,
    //     ToWhom: 2,
    //     Data: heartBeatMessage,
    //   }
    websocket.send(
      JSON.stringify({
        Data: heartBeatMessage,
        Cmd: [
          {
            ToWhom: ToWhomAll.ToHeartBeat, //web端
            Type: SocketTypeEnum.HeartBeat,
          },
        ],
      })
    );

    // websocket.send(sendMsg);
    setTimeout(() => {
      if (numHeartBeat == 1) {
        (websocket as any).onclose();
      }
    }, 2000);
  }
}
// 创建连接
function websocketOpen() {
  console.log("连接成功");
  isConnected = true;
  // sendWebSocket({
  //   Type: SocketTypeEnum.FacTitle,
  //   Data: JSON.stringify({
  //     Page: 1,
  //     Rows: 10,
  //   }),
  // });
  // store.dispatch("websocket/connect"); // 修改连接状态
  startHeartbeat();
  return true;
}

function startHeartbeat() {
  console.log("开启ws心跳");
  heartBeatInterval = setInterval(sendHeartbeat, heartBeatIntervalTime);
}

function clearHeartbeat() {
  if (heartBeatInterval) {
    clearInterval(heartBeatInterval);
    heartBeatInterval = null;
  }
}

// 数据接收
function websocketonmessage(e: MessageEvent<any>) {
  // console.log("数据接收", e.data);
  const data = JSON.parse(e.data); // 解析JSON格式的数据
  // console.log("data", data);
  // console.log(findEnumNameByValue(data.Data));

  if (data.Data === "heartbeat") {
    // 这是心跳响应，不做其他处理
    numHeartBeat = 0;
    return;
  }

  const dataInfo = JSON.parse(data.Data);
  // console.log("dataInfo", dataInfo);
  // 下面的判断则是后台返回的接收到的数据  如何处理自己决定
  if (dataInfo.code === 400) {
    // console.log("数据接收", data.msg);
    ElMessage({
      showClose: true,
      message: dataInfo.msg,
      type: "warning",
    });
  } else if (dataInfo.code === 0) {
    ElMessage({
      showClose: true,
      message: "连接成功",
      type: "success",
    });
  } else if (dataInfo.code === 200) {
    // store.commit("websocket/SET_WS_MSG", dataInfo);
    bus.emit("wsMessage", { ...dataInfo, Type: data.Type });
    // 成功后的相应处理  此处成功后播放音乐
    // const audio = new Audio("./tipMusic.mp3");
    // audio.play();
  } else {
    ElMessage({
      showClose: true,
      message: dataInfo.msg,
      type: "error",
    });
  }
  // let data = JSON.parse(decodeUnicode(e.data))
}
// 关闭
function websocketclose(e: any) {
  // console.log(e);
  // store.dispatch("websocket/disconnect"); // 修改连接状态
  // console.log("connection closed (" + e.code + ")");
  clearHeartbeat();
}
// 数据发送
function websocketsend(data: any) {
  if (websocket && isConnected) {
    console.log("发送的数据", JSON.stringify(data));
    // 检查连接状态

    websocket.send(JSON.stringify(data));
  } else {
    ElMessage({
      showClose: true,
      message: "通信连接疑似断开",
      type: "error",
    });
  }
}
// 实际调用的方法==============
// 发送
function sendWebSocket(data: any) {
  // console.log(store.state as any);

  // 如果未保持连接状态 不允许直接发送消息 提示请选择连接设备
  if (!isConnected) {
    ElMessage({
      showClose: true,
      message: "通信连接疑似断开",
      type: "error",
    });
  } else {
    websocketsend(data);
    console.log("------------------");
  }
}
// 关闭
const closeWebSocket = () => {
  if (websocket) {
    websocket.close();
    ElMessage({
      showClose: true,
      message: "设备已关闭",
      type: "success",
    });
  }
};
export {
  initWebSocket,
  sendWebSocket,
  creatWebSocket,
  closeWebSocket,
  isConnected,
};
